package com.kevin.hw.javaapi;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author: kevin
 * @date: 2017/8/14
 * @description:
 */
public class CreateSessionDemo {

    private final static String CONNECT_HOST = "localhost:2181";

    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException {

        ZooKeeper zooKeeper = new ZooKeeper(CONNECT_HOST, 5000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected){
                    try {
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("zookeeper连接状态："+watchedEvent.getState());
                    countDownLatch.countDown();
                }
            }
        });

        countDownLatch.await();

        System.out.println("zookeeper state "+zooKeeper.getState());

    }


}
